using System;
using System.Collections.Generic;
using System.Text;
using Barrage;
using Barrage.Meshes;
using Spacedash.Logic;
using SlimDX;
using Barrage.Shaders;
using Barrage.Textures;
using Barrage.Audios;

namespace Spacedash.Game.Items
{
    /// <summary>
    /// Item that shows a ship's mesh
    /// </summary>
    public class ShipItem: Item
    {
        const string SHIP_FOLDER = "Ships\\";
        Ship ship;
        /// <summary>
        /// Ship associated to this item
        /// </summary>
        public Ship Ship { get { return ship; } }

        Mesh shipMesh;
        /// <summary>
        /// Constructor. Inits meshes, materials and audio
        /// </summary>
        /// <param name="ship">Ship associated to this item</param>
        public ShipItem(Ship ship)
        {
            this.ship = ship;

            shipMesh = MeshManager.Meshes[SHIP_FOLDER+ship.Profile.ShipModelInstance.Model];
            shipMesh.ClearMaterials();
            Material material=new Material();
            material.Textures.Add(TextureManager.Textures["cellshading.dds"]);
            material.Diffuse = new Color4(ship.Color);
            material.FloatParameters.Add(0.1f);
            shipMesh.Materials.Add(material);

            shipMesh.Shader=new D3DShader("Ship.fx");

            SpatializedAudio audio = new SpatializedAudio(AudioFileManager.Audios["test.wav"]);
            audio.Loop = true;
            audio.Volume = 0;
            audio.ReferenceDistance = 50;
            addAudio(audio);
            
            addMesh(shipMesh);
        }
        /// <summary>
        /// \internal Renders the ship
        /// </summary>
        protected override void DoRender()
        {
            shipMesh.FirstMaterial.Diffuse = new Color4(ship.Color);
            base.DoRender();
        }
        /// <summary>
        /// \internal Updates audio and material information
        /// </summary>
        protected override void DoUpdate()
        {
            Audio.Speed = 0.2f + 1.5f * ship.Speed / Ship.ProfileMaxSpeed;
            Audio.Volume = 0.5f + 0.75f * ship.Speed / Ship.ProfileMaxSpeed;
            foreach (Audio a in audios)
                if (!a.Playing) a.Start();

            shipMesh.FirstMaterial.Diffuse = new Color4(ship.Color);
            base.DoUpdate();
        }

    }
}
